// void, Obj This

Unit newunit;
Barrack this;
point ptExit;
int level,i,randItem;
str nameItem;

this = This.AsBuilding();

//while(.settlement.IsFull)
	//Sleep(500);
.Progress();
//while(.settlement.IsFull)
	//Sleep(500);

randItem = rand(30);

if(randItem < 16){
	if(randItem == 0)
		nameItem = "Magic Shoes";
	else if(randItem == 1)
		nameItem = "Battle drums of rage";
	else if(randItem == 2)
		nameItem = "Harp of stamina";
	else if(randItem == 3)
			nameItem = "Sign of the hammer";
	else if(randItem == 4)            
		nameItem = "Ghost's skull";
	else if(randItem == 5)                    
		nameItem = "Wine of vigour";
	else if(randItem == 6)
		nameItem = "Watch Eye II";
	else if(randItem == 7)
		nameItem = "Assassin sword";
	else if(randItem == 8)
		nameItem = "Herb amulet of luck";
	else if(randItem == 9)
		nameItem = "King's Belt";
	else if(randItem == 10)
		nameItem = "Fur gloves of health";
	else if(randItem == 11)
		nameItem = "Finger of death";
	else if(randItem == 12)
		nameItem = "Ash of druid heart";
	else if(randItem == 13)
		nameItem = "Horn of victory";
	else if(randItem == 14)
		nameItem = "Boar teeth";
	else if(randItem == 15)
		nameItem = "Elephant Tusk"; 
}              
if(rand(100) < 3){
	level = 12+rand(17)+1;
} else if(rand(100) < 7){
	level = 8+rand(16)+1; 
} else if(rand(100) < 20){
	level = 7+rand(12)+1;
} else {
	level = 6+rand(4)+1;
}					
					
ptExit = .GetExitPoint(.settlement.GetCentralBuilding.pos, false);
if (ptExit.x==-1 && ptExit.y==-1) {
	Sleep(rand(100)+100);
	newunit = Place(GetRandomHeroClass(cmdparam, .player), Point(0,0), this.player);
	if(newunit.IsValid){
		if(EnvReadString(.settlement, "tribute_people") == "on")
			.settlement.ForceAddUnit(newunit); // wysylka w great_taverd_idle.vs
		else 
			newunit.SetCommand("move", .settlement.GetCentralBuilding.pos+Point(200+rand(300), 200+rand(300)));
	}
} else {
	Sleep(rand(100)+100);
	newunit = Place(GetRandomHeroClass(cmdparam, .player), ptExit, this.player);
	if(newunit.IsValid){
		if(EnvReadString(.settlement, "tribute_people") == "on")
			.settlement.ForceAddUnit(newunit); // wysylka w great_taverd_idle.vs
		else 
			newunit.SetCommand("move", .settlement.GetCentralBuilding.pos+Point(200+rand(300), 200+rand(300)));
	}
	
}

newunit.AddItem(nameItem);
newunit.SetLevel(level);
